/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddPersonPanel.java
 *
 * Created on 2011-11-27, 15:29:27
 */
package mscs.server.admin.view;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import mscs.client.member.view.AddMemberDialog;
import mscs.data.pojo.Person;
import mscs.server.admin.logic.AddPersonUtil;
import mscs.server.admin.logic.IAddPersonUtil;
import mscs.server.admin.view.AddPersonDialog;
import mscs.server.admin.view.TableModelFactory;
import mscs.server.view.MscsServerView;

/**
 *
 * @author Administrator
 */
public class AddPersonPanel extends javax.swing.JPanel {
    private IAddPersonUtil addutil = new AddPersonUtil();
    MscsServerView serverView;
    private DefaultTableModel tableModel = new DefaultTableModel();
    /** Creates new form AddPersonPanel */
    public AddPersonPanel(MscsServerView parent) {
        this.serverView = parent;
        initComponents();
        setEnvironment();
    }
    
    private void setEnvironment(){
        this.buttonDel.setEnabled(false);
        this.buttonEdit.setEnabled(false);
        refreshTable();
    }
    
    public void refreshTable(){
        this.tableModel.setDataVector(TableModelFactory.toListDataVector(addutil.getNewList()), 
                TableModelFactory.PERSON_COLUMN_NAMES);
        this.buttonDel.setEnabled(false);
        this.buttonEdit.setEnabled(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonAdd = new javax.swing.JButton();
        buttonDel = new javax.swing.JButton();
        buttonEdit = new javax.swing.JButton();
        buttonSubmit = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mscs.server.view.MscsServer.class).getContext().getResourceMap(AddPersonPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(tableModel);
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        buttonAdd.setText(resourceMap.getString("buttonAdd.text")); // NOI18N
        buttonAdd.setName("buttonAdd"); // NOI18N
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });

        buttonDel.setText(resourceMap.getString("buttonDel.text")); // NOI18N
        buttonDel.setName("buttonDel"); // NOI18N
        buttonDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDelActionPerformed(evt);
            }
        });

        buttonEdit.setText(resourceMap.getString("buttonEdit.text")); // NOI18N
        buttonEdit.setName("buttonEdit"); // NOI18N
        buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

        buttonSubmit.setText(resourceMap.getString("buttonSubmit.text")); // NOI18N
        buttonSubmit.setName("buttonSubmit"); // NOI18N
        buttonSubmit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonSubmitMouseClicked(evt);
            }
        });
        buttonSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSubmitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                        .addComponent(buttonSubmit)
                        .addGap(8, 8, 8))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAdd)
                    .addComponent(buttonDel)
                    .addComponent(buttonSubmit)
                    .addComponent(buttonEdit))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
    AddPersonDialog addDialog = new AddPersonDialog(this.serverView.getFrame(), true);
    Person person = addDialog.showDailog();
    if(person != null  ){
        if(this.addutil.addToNewList(person)){
            refreshTable();
        }else{
            JOptionPane.showMessageDialog(this.buttonAdd, "重复工号");
        }
    }
    
}//GEN-LAST:event_buttonAddActionPerformed

private void buttonSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSubmitActionPerformed
    if(this.addutil.commitNewList()){
        JOptionPane.showMessageDialog(this, "提交成功");
    }
    refreshTable();
}//GEN-LAST:event_buttonSubmitActionPerformed

private void buttonSubmitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonSubmitMouseClicked
}//GEN-LAST:event_buttonSubmitMouseClicked

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
    if(this.jTable1.getSelectedRowCount()!=0){
        this.buttonDel.setEnabled(true);
        this.buttonEdit.setEnabled(true);
    }
}//GEN-LAST:event_jTable1MouseClicked

private void buttonDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDelActionPerformed
    this.addutil.removeFromNewList(this.jTable1.getSelectedRow());
    refreshTable();
}//GEN-LAST:event_buttonDelActionPerformed

private void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
    AddPersonDialog editDialog = new AddPersonDialog(this.serverView.getFrame(),true,
            this.addutil.getByIndex(this.jTable1.getSelectedRow()));
    Person person = editDialog.showDailog();
    if(person != null  ){
            this.addutil.setByIndex(this.jTable1.getSelectedRow(), person);
            refreshTable();
    }
}//GEN-LAST:event_buttonEditActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonDel;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JButton buttonSubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
